/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2019
 */

package com.bm.console.action;

import java.util.List;

import com.bm.console.business.WebCityService;
import com.bm.console.pojo.WebCityItem;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bm.console.business.WebCityTemperatureService;
import com.bm.console.pojo.WebCityTemperature;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultException;
import com.sys.object.common.ObjResultJSON;
import com.sys.object.common.page.PageBean;

@Controller
public class WebCityTemperatureAction {
    @Resource
    WebCityService webCityService;

    @Resource
    WebCityTemperatureService webCityTemperatureService;

    @RequestMapping(value = "/webCityTemperatureList.do")
    public String webCityTemperatureList(PageBean pageBean, Model model, WebCityTemperature webCityTemperature) {
        try {
            pageBean = webCityTemperatureService.queryPageList(webCityTemperature, pageBean);
            model.addAttribute("pageBean", pageBean);
            model.addAttribute("startRow", pageBean.getStartRow());
            model.addAttribute("entity", webCityTemperature);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/web/webcitytemperature/list";
    }

    /**
     * 新增动态 Get
     */
    @RequestMapping(value = "/addWebCityTemperature.do", method = RequestMethod.GET)
    public String addWebCityTemperature(@ModelAttribute("webCityTemperature") WebCityTemperature webCityTemperature, Model model, String id) {
        Integer prow = 0;
        try {
            if (StringUtils.hasText(id)) {
                int key = Integer.parseInt(id);
                if (key > 0) {
                    WebCityTemperature record = webCityTemperatureService.queryByPrimaryKey(key);
                    model.addAttribute("entity", record);
                }
            }

            List<WebCityItem> city_list = webCityService.selectAll();
            model.addAttribute("city_list", city_list);

        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("prow", prow);
        return "/web/webcitytemperature/add";
    }

    /**
     * 新增动态 Post
     */
    @RequestMapping(value = "/saveWebCityTemperature.do", method = RequestMethod.POST)
    public String saveWebCityTemperature(@Validated @ModelAttribute("webCityTemperature") WebCityTemperature webCityTemperature,
                                         BindingResult result, Model model, Integer prow,
                                         HttpServletRequest request) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            System.err.println("webCityTemperature:"+webCityTemperature.toString());
            int success = webCityTemperatureService.saveOrUpdate(webCityTemperature);
            System.err.println("success:"+success);
            if(success == -1){
                ret.setResultCode(ObjResultCode.HAVE_EXISTED);
            }else if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            }
        } catch (Exception e) {

            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
            e.printStackTrace();
            model.addAttribute("entity", webCityTemperature);
            model.addAttribute("ret", ret);
            return "/web/webcitytemperature/add";
        }
        return "redirect:/webCityTemperatureList.do?status=";
    }

    /**
     * 新增动态 Post
     */
    @RequestMapping(value = "/saveWebCityTemperatureJson.do", method = RequestMethod.POST)
    @ResponseBody
    public ObjResultException<String> saveWebCityTemperatureJson(@RequestBody WebCityTemperature webCityTemperature, Model model,
                                                                 HttpServletRequest request) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webCityTemperatureService.saveOrUpdate(webCityTemperature);
            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
        }
        return ret;
    }

    /**
     * 删除APP
     */
    @RequestMapping("/deleteWebCityTemperature.do")
    @ResponseBody
    public ObjResultException<String> deleteWebCityTemperature(Integer id) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webCityTemperatureService.delete(id);

            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 修改状态APP
     */
    @RequestMapping("/updateStatusWebCityTemperature.do")
    @ResponseBody
    public ObjResultException<String> updateStatusWebCityTemperature(WebCityTemperature webCityTemperature) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webCityTemperatureService.updateStatus(webCityTemperature);

            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
        }
        return ret;
    }
}